package com.quartet.modules.vehicle.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.quartet.modules.vehicle.dao.TTerminalEquipmentInformationDao;
import com.quartet.modules.vehicle.dto.TTerminalEquipmentInformationAddRequestDto;
import com.quartet.modules.vehicle.entity.TTerminalEquipmentInformation;
import com.quartet.modules.vehicle.service.TTerminalEquipmentInformationService;
import com.quartet.utils.JsonTool;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.List;

/**
 * 终端设备信息表(TTerminalEquipmentInformation)表服务实现类
 *
 * @author makejava
 * @since 2021-05-04 16:54:24
 */
@Service("tTerminalEquipmentInformationService")
public class TTerminalEquipmentInformationServiceImpl extends ServiceImpl<TTerminalEquipmentInformationDao, TTerminalEquipmentInformation> implements TTerminalEquipmentInformationService {

    /**
     * 添加终端设备信息
     *
     * @param tTerminalEquipmentInformationAddRequestDto
     */
    @Override
    public void addTerminalEquipmentInformation(TTerminalEquipmentInformationAddRequestDto tTerminalEquipmentInformationAddRequestDto) {
        if (!ObjectUtils.isEmpty(tTerminalEquipmentInformationAddRequestDto)) {
            TTerminalEquipmentInformation tTerminalEquipmentInformation = BeanUtil.copyProperties(tTerminalEquipmentInformationAddRequestDto, TTerminalEquipmentInformation.class);
            tTerminalEquipmentInformation.setTerminalExternalEquipment(JsonTool.beanToJson(tTerminalEquipmentInformationAddRequestDto.getTerminalExternalEquipment()));
            tTerminalEquipmentInformation.setTerminalAlarmJudgment(JsonTool.beanToJson(tTerminalEquipmentInformationAddRequestDto.getTerminalAlarmJudgment()));
            this.baseMapper.insert(tTerminalEquipmentInformation);
        }
    }

    /**
     * 功能描述: 修改终端设备信息
     * @author huipanxing huipanxing@chinasofti.com
     * @date 2021/5/13 16:16
     * @param tTerminalEquipmentInformationAddRequestDto
     * @return void
     */
    @Override
    public void updateTerminalEquipmentInformation(TTerminalEquipmentInformationAddRequestDto tTerminalEquipmentInformationAddRequestDto) {
        if (!ObjectUtils.isEmpty(tTerminalEquipmentInformationAddRequestDto)) {
            TTerminalEquipmentInformation tTerminalEquipmentInformation = BeanUtil.copyProperties(tTerminalEquipmentInformationAddRequestDto, TTerminalEquipmentInformation.class);
            tTerminalEquipmentInformation.setTerminalExternalEquipment(JsonTool.beanToJson(tTerminalEquipmentInformationAddRequestDto.getTerminalExternalEquipment()));
            tTerminalEquipmentInformation.setTerminalAlarmJudgment(JsonTool.beanToJson(tTerminalEquipmentInformationAddRequestDto.getTerminalAlarmJudgment()));
            this.baseMapper.updateById(tTerminalEquipmentInformation);
        }
    }

    /**
     * 根据车辆编码删除终端设备信息
     * @param vehicleCode
     */
    @Override
    public void deleteTerminalEquipmentInformation(String vehicleCode) {
        this.baseMapper.delete(new QueryWrapper<TTerminalEquipmentInformation>().lambda().eq(TTerminalEquipmentInformation::getVehicleCode,vehicleCode));
    }

    /**
     * 根据车辆编码查询终端设备信息
     * @param vehicleCode
     * @return
     */
    @Override
    public TTerminalEquipmentInformationAddRequestDto selectTerminalEquipmentInformation(String vehicleCode) {
        TTerminalEquipmentInformation tTerminalEquipmentInformation = this.baseMapper.selectOne(new QueryWrapper<TTerminalEquipmentInformation>().lambda().eq(TTerminalEquipmentInformation::getVehicleCode, vehicleCode));
        TTerminalEquipmentInformationAddRequestDto tTerminalEquipmentInformationAddRequestDto = BeanUtil.copyProperties(tTerminalEquipmentInformation, TTerminalEquipmentInformationAddRequestDto.class);
        if (!ObjectUtils.isEmpty(tTerminalEquipmentInformation.getTerminalExternalEquipment())) {
            List<String> terminalExternalEquipmentList = JSON.parseArray(tTerminalEquipmentInformation.getTerminalExternalEquipment().toString(), String.class);
            tTerminalEquipmentInformationAddRequestDto.setTerminalExternalEquipment(terminalExternalEquipmentList);
        }
        if (!ObjectUtils.isEmpty(tTerminalEquipmentInformation.getTerminalAlarmJudgment())) {
            List<String> terminalAlarmJudgmentList = JSON.parseArray(tTerminalEquipmentInformation.getTerminalAlarmJudgment().toString(), String.class);
            tTerminalEquipmentInformationAddRequestDto.setTerminalAlarmJudgment(terminalAlarmJudgmentList);
        }
        return tTerminalEquipmentInformationAddRequestDto;
    }
}
